package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class text {
    public static void main1(String[] args){
        try{
            //获取class对象的三种方法：
            Class<?> c1=Class.forName("reflectdemo.Student");
            Class c2=Student.class;
            Student student=new Student();
            Class<?> c3=student.getClass();
            System.out.println(c1.equals(c2));
            System.out.println(c1.equals(c3));
            System.out.println(c2.equals(c3));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateMethod(){
        try{
            Class<?> c1=Class.forName("reflectdemo.Student");
            Method method=c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            Student student=(Student)c1.newInstance();
            method.invoke(student,"私有方法的使用...");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateField(){
        try{
            Class<?> c1=Class.forName("reflectdemo.Student");
            Field field= c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student=(Student)c1.newInstance();
            field.set(student,"小笨茜");
            System.out.println(student);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static void reflectPrivateConstructor(){
        try{
            Class<?> c1=Class.forName("reflectdemo.Student");
            Constructor<Student> constructor=
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class ,int.class);
            constructor.setAccessible(true);
            Student student=constructor.newInstance("小笨",19);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectNewInstance(){
        try{
            Class<?> c1=Class.forName("reflectdemo.Student");
            Student student=(Student) c1.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args){
       // reflectPrivateMethod();//反射一个私有方法：
       // reflectPrivateField();//私有属性的使用；
        //reflectPrivateConstructor();//反射一个私有的构造方法;
        reflectNewInstance();//反射一个新的接口；
    }

}
